package com.java28.rbac.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java28.rbac.pojo.User;
import com.java28.rbac.pojo.UserExample;
import com.java28.rbac.pojo.UserExample.Criteria;
import com.java28.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("index")
    public String userView(){
        return "WEB-INF/user/user";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pageSize",defaultValue = "5")Integer pageSize,
                                   String like){
        Map<String,Object> map = new HashMap<String,Object>();
//        进行pageHelper分页
        PageHelper.startPage(pageNum,pageSize);
//        设置查询条件
//        进行查询
        List<User> userList = userService.findAll(like);
//        进行分页
        PageInfo<User> pageInfo = new PageInfo<User>(userList);

        map.put("userList",userList);
        map.put("pageInfo",pageInfo);
        return map;
    }


    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add(User user){
        System.out.println(user);
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            userService.save(user);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> delete(Integer userid){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            userService.deleteOne(userid);
            map.put("result",true);
        }catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    @RequestMapping("updateView")
    public String updateView(Integer userid, Model model){
        User user = userService.findOne(userid);
        model.addAttribute("user",user);
        return "WEB-INF/user/update";
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(User user){
        System.out.println(user);
        try{
            userService.update(user);
        }catch(Exception e){
            return "false";
        }
        return "true";
    }


    @RequestMapping("deleteAll")
    @ResponseBody
    public String deleteAll(Integer[] userid){
        try{
            userService.deleteAll(userid);
        }catch(Exception e){
            return "false";
        }
        return "true";
    }


}
